package com.omenkk.juc.synchronizedPlus;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class dmeo4 {
    public static void main(String[] args) throws InterruptedException {
        for(int i=1;i<=3;i++){
            People people=new People();
         new Thread(people,"线程"+i+"号").start();
        }
        Thread.sleep(1000);
        log.debug("{}",Mailboxes.getIds());
        for (Integer id : Mailboxes.getIds()) {
            Postman postman=new Postman(id,id+"号，你好，这是给你的信");
            new Thread(postman,"送"+id+"号信").start();
        }
    }
}
@Slf4j
class People implements Runnable{

    @Override
    public void run() {
        GuardedObject guardedObject=Mailboxes.createGuarded();
        log.debug("开始收信:{}",guardedObject.getId());
        Object o = null;
        try {
            o = guardedObject.get(5000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        log.debug("收到信，内容:{}",o);
    }
}

@Slf4j
class Postman implements Runnable{
private int id;
private String mail;

    public Postman(int id, String mail) {
        this.id = id;
        this.mail = mail;
    }

    @Override
    public void run() {
        GuardedObject guarder = Mailboxes.getGuarder(this.id);
        log.debug("送信，id:{},内容:{}",id,mail);
        guarder.complete(mail);
    }
}
